package bsc.war.populate;

import bsc.ejb.entity.ColorimetrySettings;
import bsc.ejb.entity.Map;
import bsc.ejb.entity.MapDrawingSettings;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.PersonScoreDomain;
import bsc.ejb.entity.Pointer;
import bsc.ejb.entity.SRM;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.ScoreDomain;
import bsc.ejb.entity.ScoreView;
import bsc.ejb.entity.Settings;
import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.MapDrawing;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class SettingsPopulate extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

        ScoreCard scoreCardRoot = delegate.findScoreCard(19L);

        Person personDesigner = delegate.findPerson(1L);
        ScoreDomain scoreDomainDesigner = delegate.findScoreDomain(1L);

        Person person = delegate.findPerson(2L);
        ScoreDomain scoreDomain = delegate.findScoreDomain(2L);



        //SE ASOCIA EL VIEW
        ScoreView scoreView = (ScoreView) Factory.getBean("ScoreView");
        scoreView.setName("Néctares del Sureste");
        scoreView.setDescription("Vista que contiene los Scorecards de Néctares del Sureste");
        scoreView.setScoreCard(scoreCardRoot);
        scoreView.setScoreDomain(scoreDomain);
        delegate.createScoreView(scoreView);

        scoreCardRoot.setScoreview(scoreView);
        delegate.updateScoreCard(scoreCardRoot);

        //Se updatea el dominio

        scoreDomain.setScoreView(scoreView);
        delegate.updateScoreDomain(scoreDomain);


        //Se asocia persona al dominio

        PersonScoreDomain personScoreDomainDesigner = (PersonScoreDomain) Factory.getBean("PersonScoreDomain");
        personScoreDomainDesigner.setPerson(personDesigner);
        personScoreDomainDesigner.setScoreDomain(scoreDomainDesigner);
        personScoreDomainDesigner.setProfile(PersonScoreDomain.Profile.DESIGNER);
        delegate.createPersonScoreDomain(personScoreDomainDesigner);

        PersonScoreDomain personScoreDomain = (PersonScoreDomain) Factory.getBean("PersonScoreDomain");
        personScoreDomain.setPerson(person);
        personScoreDomain.setScoreDomain(scoreDomain);
        personScoreDomain.setProfile(PersonScoreDomain.Profile.CORE_TEAM);
        delegate.createPersonScoreDomain(personScoreDomain);

        

        //CAMBIOS GENERALES -----------------------------------------------------------------
        //Se agregan Settings a todos los Scorecards

        List<ScoreCard> listScoreCards = delegate.findScoreCard();
        //Settings Scorecards

        for (ScoreCard scoreCard : listScoreCards) {
            scoreCard.setResponsible(person);


            if (!(scoreCard instanceof Pointer)) {
                Settings setting = (Settings) Factory.getBean("Settings");
                setting.setType(Settings.Type.SCORECARD);
                setting.setScoreCard(scoreCard);
                delegate.createSettings(setting);

                scoreCard.setUnit("(%)");
                scoreCard.setSettings(setting);
              

            }
             delegate.updateScoreCard(scoreCard);

        }

        //SE AGREGAN RANGOS A TODOS LOS SCORECARDS
        //Rangos
        List<ScoreCard> listScoreCardsRangos = delegate.findScoreCard();
        Double arrayd[] = {100000D, .9D, .8D, -100000D};

        for (ScoreCard scoreCard : listScoreCardsRangos) {
            Settings settingSC = scoreCard.getSettings();

            int cont = 1;
            for (long j = 1; j <= 3; j++) {
                Colorimetry colorimetry = delegate.findColorimetry(j);
                ColorimetrySettings cs = (ColorimetrySettings) Factory.getBean("ColorimetrySettings");
                cs.setSettings(settingSC);
                cs.setColorimetry(colorimetry);
                cs.setMaxScore(arrayd[cont - 1]);
                cs.setMinScore(arrayd[cont]);
                delegate.createColorimetrySettings(cs);
                cont++;
            }
        }

        //Se crea el MAPA!!!!!
        //MapDrawing
        MapDrawing mapDrawing = (MapDrawing) delegate.findMapDrawing(1L);

        //Map
        Map map = (Map) delegate.findScoreCard(19L);

        map.setMapDrawing(mapDrawing);
        delegate.updateMap(map);

        mapDrawing.setMap(map);
        delegate.updateMapDrawing(mapDrawing);


        //Se agregan scorecards al MAPA



        //MapDrawingSettings
        ScoreCard objectiveScorecardF3 = delegate.findScoreCard(3L);

        MapDrawingSettings mpsF3 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsF3.setSettings(objectiveScorecardF3.getSettings());
        mpsF3.setMapDrawing(mapDrawing);
        mpsF3.setX(445D);
        mpsF3.setY(125D);
        delegate.createMapDrawingSettings(mpsF3);

        ScoreCard objectiveScorecardF4 = delegate.findScoreCard(11L);

        MapDrawingSettings mpsF4 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsF4.setSettings(objectiveScorecardF4.getSettings());
        mpsF4.setMapDrawing(mapDrawing);
        mpsF4.setX(650D);
        mpsF4.setY(125D);
        delegate.createMapDrawingSettings(mpsF4);



        ScoreCard objectiveScorecardC1 = delegate.findScoreCard(12L);

        MapDrawingSettings mpsC1 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsC1.setSettings(objectiveScorecardC1.getSettings());
        mpsC1.setMapDrawing(mapDrawing);
        mpsC1.setX(190D);
        mpsC1.setY(215D);
        delegate.createMapDrawingSettings(mpsC1);


        ScoreCard objectiveScorecardP1 = delegate.findScoreCard(13L);

        MapDrawingSettings mpsP1 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsP1.setSettings(objectiveScorecardP1.getSettings());
        mpsP1.setMapDrawing(mapDrawing);
        mpsP1.setX(245D);
        mpsP1.setY(320D);
        delegate.createMapDrawingSettings(mpsP1);


        ScoreCard objectiveScorecardA1 = delegate.findScoreCard(14L);

        MapDrawingSettings mpsA1 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mpsA1.setSettings(objectiveScorecardA1.getSettings());
        mpsA1.setMapDrawing(mapDrawing);
        mpsA1.setX(215D);
        mpsA1.setY(495D);
        delegate.createMapDrawingSettings(mpsA1);



        //SE CREA SRM

        SRM srm1 = (SRM) Factory.getBean("SRM");
        srm1.setName("RAE de Junio");
        srm1.setPlace("Sala de Juntas 1");
        srm1.setResponsable("SMO de Néctares del Sureste");
        srm1.setExpectedTime(2D);
        srm1.setSRMDate(ScoreCardUtils.generateDate(15, 8, 2011));
        srm1.setScoreView(scoreView);
        delegate.createSRM(srm1);

        /*Topic topic1 = (Topic) Factory.getBean("Topic");
        topic1.setDescription("Tema de Prueba");
        delegate.createTopic(topic1);

        TopicScoreView topicScoreView1 = (TopicScoreView) Factory.getBean("TopicScoreView");
        topicScoreView1.setScoreView(scoreView);
        topicScoreView1.setTopic(topic1);
        topicScoreView1.setType(TopicScoreView.Type.DELAYED);
        delegate.createTopicScoreView(topicScoreView1);*/


        return new ModelAndView("advancePopulate");
    }
}
